package com.huiztech.maystall.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huiztech.maystall.activity.ProductDetailActivity;
import com.huiztech.maystall.activity.R;
import com.huiztech.maystall.activity.R.color;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

public class WaterfallAdapter extends BaseAdapter {

    List<Map<String, Object>> list;
    Context context;
    private Drawable drawable;

    class Holder {
        public ImageView imageView, noImageView;
        public TextView contentTextView;
        public TextView priceTextView;
        public TextView likeTextView;
        public ProgressBar loadingBar;
        public LinearLayout layout,mainLayout;
        public RelativeLayout relativeLayout;
    }

    public WaterfallAdapter(List<Map<String, Object>> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup group) {
        final Holder holder;
        // 得到View
        if (view == null) {
            holder = new Holder();
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.main_list_item, null);
            holder.imageView = (ImageView) view.findViewById(R.id.main_item_image);
            holder.priceTextView = (TextView) view.findViewById(R.id.main_item_price_text);
            holder.likeTextView = (TextView) view.findViewById(R.id.main_item_like_text);
            holder.noImageView = (ImageView) view.findViewById(R.id.main_item_like_image);
            holder.contentTextView = (TextView) view.findViewById(R.id.main_item_content_text);
            holder.loadingBar = (ProgressBar) view.findViewById(R.id.main_item_load);
            holder.layout = (LinearLayout) view.findViewById(R.id.main_list_layout);
            holder.mainLayout = (LinearLayout) view.findViewById(R.id.main_list_item_layout);
            holder.relativeLayout = (RelativeLayout) view.findViewById(R.id.main_item_layout);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        holder.layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ProductDetailActivity.class);
                intent.putExtra("image", list.get(position).get("image").toString());
                context.startActivity(intent);
            }
        });

        if (position == 0) {
            holder.relativeLayout.setVisibility(View.GONE);
            holder.mainLayout.setBackgroundResource(0);
            holder.contentTextView.setVisibility(View.GONE);
        }else{
            holder.mainLayout.setBackgroundResource(R.drawable.main_product_bg);
            holder.relativeLayout.setVisibility(View.VISIBLE);
            holder.contentTextView.setVisibility(View.VISIBLE);
            
            holder.likeTextView.setText(list.get(position).get("like").toString());
            holder.contentTextView.setText(list.get(position).get("intro").toString());
            holder.priceTextView.setText("M.$ " + list.get(position).get("price").toString());
        }
       
        holder.imageView.setBackgroundResource((Integer) list.get(position).get("image"));
        holder.loadingBar.setVisibility(View.GONE);

        // ImageLoader.getInstance().displayImage(list.get(position).get("image").toString(),
        // holder.imageView, new ImageLoadingListener() {
        //
        // @Override
        // public void onLoadingStarted(String arg0, View arg1) {
        // holder.loadingBar.setVisibility(View.VISIBLE);
        // }
        //
        // @Override
        // public void onLoadingFailed(String imageUri, View view,
        // FailReason failReason) {
        // String message = null;
        // switch (failReason.getType()) {
        // case IO_ERROR:
        // message = "Input/Output error";
        // break;
        // case DECODING_ERROR:
        // message = "can not be decoding";
        // break;
        // case NETWORK_DENIED:
        // message = "Downloads are denied";
        // break;
        // case OUT_OF_MEMORY:
        // message = "内存不足";
        // Toast.makeText(context, message, Toast.LENGTH_SHORT)
        // .show();
        // break;
        // case UNKNOWN:
        // message = "Unknown error";
        // Toast.makeText(context, message, Toast.LENGTH_SHORT)
        // .show();
        // break;
        // }
        // holder.loadingBar.setVisibility(View.GONE);
        // }
        //
        // @Override
        // public void onLoadingComplete(String arg0, View arg1,
        // Bitmap arg2) {
        // holder.loadingBar.setVisibility(View.GONE);
        // }
        //
        // @Override
        // public void onLoadingCancelled(String arg0, View arg1) {
        //
        // }
        // });

        return view;
    }

}
